<?php

/**
 * ventaboletos actions.
 *
 * @package    SistemaRutasEc
 * @subpackage ventaboletos
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ventaboletosActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->boletos = Doctrine_Core::getTable('boleto')
                ->createQuery('a')
                ->execute();
    }

    public function executeShow(sfWebRequest $request) {
        $this->boleto = Doctrine_Core::getTable('boleto')->find(array($request->getParameter('id_bo')));
        $this->forward404Unless($this->boleto);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new boletoForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new boletoForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($boleto = Doctrine_Core::getTable('boleto')->find(array($request->getParameter('id_bo'))), sprintf('Object boleto does not exist (%s).', $request->getParameter('id_bo')));
        $this->form = new boletoForm($boleto);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($boleto = Doctrine_Core::getTable('boleto')->find(array($request->getParameter('id_bo'))), sprintf('Object boleto does not exist (%s).', $request->getParameter('id_bo')));
        $this->form = new boletoForm($boleto);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($boleto = Doctrine_Core::getTable('boleto')->find(array($request->getParameter('id_bo'))), sprintf('Object boleto does not exist (%s).', $request->getParameter('id_bo')));
        $boleto->delete();

        $this->redirect('ventaboletos/index');
    }

    public function executeListado(sfWebRequest $request) {
        $nombre = '';
        if ($this->getUser()->getGuardUser()->hasPermission('union')) {
            $nombre = 'Union Cariamanga'; //FIXME
        } else if ($this->getUser()->getGuardUser()->hasPermission('loja')) {
            $nombre = 'Loja Internacional'; //FIXME
        }

        $cooperativa = Doctrine_Core::getTable('Cooperativa')->createQuery('c')
                ->where('c.nombre = ?', $nombre)
                ->fetchOne();

        $this->rutas = Doctrine_Core::getTable('ruta')->createQuery('r')
                ->where('r.id_co = ?', $cooperativa->id_co)
                ->execute();

        if ($request->isMethod('POST')) {
            $totalhoras = explode("-", $request->getParameter('frecuencia'));

            $hora = $totalhoras[0];
            $this->turnos = Doctrine_Core::getTable('turno')->createQuery('t')
                    ->where('t.id_ru = ?', $request->getParameter('ruta'))
                    ->andWhere('t.fecha = ?', $request->getParameter('fecha'))
                    //->andWhere('t.hora = ?', $hora)
                    ->andWhere('t.id_co = ?', $cooperativa->id_co)
                    ->execute();
        }
    }

    public function executeImprimir(sfWebRequest $request) {
        $id_tu = $request->getParameter('id_tu');
        $boletos = TurnoTable::getInstance()->obtenerListaPasajeros($id_tu);
        $turno = TurnoTable::getInstance()->find($id_tu);
        $ruta = RutaTable::getInstance()->find($turno->id_ru);
        $bus = Doctrine_Core::getTable('bus')->createQuery('b')->where('b.id_bu = ?', $turno->id_bu)->fetchOne();


        $cooperativa = CooperativaTable::getInstance()->find($turno->id_co);
        $config = sfTCPDFPluginConfigHandler::loadConfig();
        $pdf = new sfTCPDF();
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('Sistema de reservaciones');
        $pdf->SetTitle('Lista de pasajeros');
        $pdf->SetSubject('Lista de pasajeros');

        $pdf->SetHeaderFont(array('dejavusans', '', 7, '', true));
        $pdf->SetHeaderData('', 0, 'Sistema de reservaciones', date('Y-m-d'));

        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        $pdf->setFontSubsetting(true);

        $pdf->SetFont('dejavusans', '', 14, '', true);

        $pdf->AddPage();

        $pdf->write(0, 'COOPERATIVA DE TRANSPORTES ' . strtoupper($cooperativa->nombre), '', false, 'C');
        $pdf->Ln();
        $pdf->write(0, 'LISTA DE PASAJEROS', '', false, 'C');
        $pdf->Ln(15);

        $pdf->SetX(35);
        $pdf->SetFont('', 'B', 10);
        $pdf->Cell(35, 0, 'Salida:');
        $pdf->SetFont('', '');
        $pdf->Cell(35, 0, $ruta->salida);
        $pdf->SetFont('', 'B');
        $pdf->Cell(35, 0, 'Destino:');
        $pdf->SetFont('', '');
        $pdf->Cell(35, 0, $ruta->llegada);
        $pdf->Ln();

        $pdf->SetX(35);
        $pdf->SetFont('', 'B');
        $pdf->Cell(35, 0, 'Fecha:');
        $pdf->SetFont('', '');
        $pdf->Cell(35, 0, $turno->fecha);
        $pdf->SetFont('', 'B');
        $pdf->Cell(35, 0, 'Bus Nº:');
        $pdf->SetFont('', '');
        $pdf->Cell(35, 0, $bus->disco);
        $pdf->Ln();


        $pdf->SetX(35);
        $pdf->SetFont('', 'B');
        $pdf->Cell(35, 0, 'Hora de salida:');
        $pdf->SetFont('', '');
        $pdf->Cell(35, 0, $turno->hora);
        $pdf->SetFont('', 'B');
        $pdf->Cell(35, 0, 'Placas:');
        $pdf->SetFont('', '');
        $pdf->Cell(35, 0, $bus->placas);

        $pdf->Ln(15);

        $pdf->SetDrawColor(0, 0, 0);
        $pdf->SetLineWidth(0.2);
        $pdf->SetFont('', 'B');

        $pdf->Cell(30, 0, 'Número', 1, 0, 'C');
        $pdf->Cell(30, 0, 'Nombres', 1, 0, 'C');
        $pdf->Cell(30, 0, 'Identificación', 1, 0, 'C');
        $pdf->Cell(30, 0, 'Destino', 1, 0, 'C');
        $pdf->Cell(30, 0, 'Asientos', 1, 0, 'C');
        $pdf->Cell(30, 0, 'Valor', 1, 0, 'C');        
        $pdf->Ln();

        $pdf->SetFont('', '');
        foreach ($boletos as $boleto) {
            $pdf->Cell(30, 0, $boleto->id_bo, 1, 0, 'C');
            $pdf->Cell(30, 0, $boleto->getPersona()->first_name . ' ' . $boleto->getPersona()->last_name, 1, 0, 'C');
            $pdf->Cell(30, 0, $boleto->getPersona()->dni, 1, 0, 'C');
            $pdf->Cell(30, 0, $boleto->destino, 1, 0, 'C');
            $pdf->Cell(30, 0, implode('-', $boleto->getAsientos()->toKeyValueArray('id_as', 'numero')), 1, 0, 'C');
            $asientos = Doctrine_Core::getTable('asiento')->createQuery('a')->where('a.id_bo = ?', $boleto->id_bo)->execute();
            $pdf->Cell(30, 0, '$ ' . $boleto->valor*count($asientos), 1, 0, 'C');
            //$pdf->Cell(30, 0, '$ ' . $boleto->valor, 1, 0, 'C');            
            $pdf->Ln();
        }



        $pdf->Output('pasajeros_turno_' . $id_tu . '.pdf', 'I');

        throw new sfStopException();
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $boleto = $form->save();

            $this->redirect('ventaboletos/edit?id_bo=' . $boleto->getIdBo());
        }
    }

}
